/*
Copyright 2009 Peter Krajnc, SCREENWORKER

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.screenworker.compactmvc.extensions.remotingservice
{
	import com.screenworker.compactmvc.extensions.remotingservice.interfaces.IRemotingService;
	import com.screenworker.compactmvc.patterns.Controller;
	import com.screenworker.compactmvc.patterns.Model;
	
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.NetConnection;
	import flash.net.ObjectEncoding;
	import flash.net.Responder;
	
	public class RemotingService extends Model implements IRemotingService
	{
		public static var NAME:String = 'RemotingService';
		private var _myService:NetConnection;
		private var _responder:Responder;
		private var _gateway:String;

		public function RemotingService(name:String, controller:Controller)
		{
			super(name, controller);
			
			_responder = new Responder(responderResultHandler, responderStatusHandler);
			
			//NET ONNECTION
			NetConnection.defaultObjectEncoding = ObjectEncoding.AMF3;
			_myService = new NetConnection();
			_myService.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			_myService.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);

		}	
		
		/**
		 * Invokes a command or method 
		 * @param classMethod
		 * @param arguments
		 * @param mockUp add a mockup object here to simulate the result
		 * 
		 */			
		protected function call(classMethod:String, arguments:Object=null, mockUp:Object=null):void
		{
			if(mockUp == null)
			{
     	 		_myService.call(classMethod, _responder, arguments ); 
   			}
     	 	else
     	 	{ 	 			
     	 		responderResultHandler(mockUp);
     	 	}
		}
		
		private function responderResultHandler(result:Object):void
		{
			 resultHandler(result);
		}
				
		private function responderStatusHandler(f:Object):void
		{
			errorHandler('RESPONDER STATUS: ' + f.description);
		}
				
		private function netStatusHandler(e:NetStatusEvent):void
		{
			statusHandler(e.info.code);
		}
			
		private function securityErrorHandler(e:SecurityErrorEvent):void
		{
			errorHandler('SECURITY ERROR: ' + e.text);
		}
		
		public function reset():void
		{
			_myService.close();
			_myService.removeEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			_myService.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			_myService = null;
			_responder = null;		
		}
		
		public function errorHandler(e:String):void
		{
			//overriden by subClass
		}
		
		public function resultHandler(result:Object):void
		{
			//overriden by subClass
		}
		
		public function statusHandler(message:String):void
		{
			//overriden by subClass
		}
		
		public function set gateway(value:String):void
		{
			_myService.close();
			
			_gateway = value;
			
			try
			{
				_myService.connect(gateway);
			}
			catch(e:Error)
			{
				errorHandler(e.message);
			}
		}
		
		public function get gateway():String
		{
			return _gateway;
		}	
	}
}
